<?php

class GzController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','import'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

    public function actionImport() {
        $model = new Gz;
        //$file = CUploadedFile::getInstance($model,'csv_file');
        // 简体
        //setlocale(LC_ALL, 'utf8');
        if(isset($_POST['Gz']))
        {
            $model->attributes=$_POST['Gz'];
            if(!empty($_FILES['Gz']['tmp_name']['tmps1']))
            {
                $file = CUploadedFile::getInstance($model,'tmps1');
                //echo 'start';
                $fp = fopen($file->tempName, 'r');
                if($fp)
                {
                    //  $line = fgetcsv($fp, 1000, ",");
                    //  print_r($line); exit;
                     $first_time = true;
                    do {
                       
                        if ($first_time == true) {
                            $first_time = false;
                            continue;
                        } 
                        //部门名称,职员姓名,微信号,标准工资,病事假扣款,工龄补贴,补款,应付工资,车补,独补,托费,女补,应发工资,养老保险,失业保险,医疗保险,公积金,个税,其它扣款,活动经费,实发工资
                        //部门大类	职员姓名	标准工资	满勤天数	出勤天数	本月基本工资	病事假扣款	工龄补贴	技能津贴	奖金	补款	应付工资	车补	独补	托费	女补	应发工资	养老保险	失业保险	医疗保险	公积金	个税	其它扣款	活动经费	实发工资	帐号后四位	身份证
                        //部门大类,职员姓名,标准工资,满勤天数,出勤天数,本月基本工资,病事假扣款,工龄补贴,技能津贴,奖金,补款,  应付工资,车补,独补,托费,女补,应发工资,养老保险,失业保险,医疗保险,公积金,个税,其它扣款,活动经费,实发工资,帐号后四位,身份证
                        //tmps2,name,bzgz,mqdays,cqdays,               jbgz,        tmps4,     glbt,    tmps7,   tmps8,tmps5,yfagz,   cb,  db,  tf,  nb,  yfagz,   yalbx,   sybx,    yilbx,   gjj,   gs,  tmps6,   hdjf,    sfgz    ,tmpi1     ,tmpi2
                        //tmps2,name,tmps3,bzgz,tmps4,glbt,tmps5,yfugz,cb,db,tf,nb,yfagz,yalbx,sybx,yilbx,gjj,gs,tmps6,hdjf,sfgz  
                        //echo $line[0].'<BR />';
                        $this->insertGz($line[0],$model->year,$model->month);
        
                    }while( ($line = fgetcsv($fp, 1000, ";")) != FALSE);
                    //$this->redirect('././index');
                    echo '上传成功';
                    $msg = $model->year.'年'.$model->month .'月工资已经更新,请输入****####，工资卡后4位和身份证后4位(eg.12347890)查询工资卡后4位为1234和身份证后4位为7890最新的工资条。\n';
                    $control=Yii::app()->runController('site/newgz/msg/'.$msg);  
                }
            }
        }
        //setlocale(LC_ALL, null);
        $this->render('csv_form', array('model' => $model) );
    }
    //getAttribute
    //'tmps2','name','bzgz','mqdays','cqdays','jbgz','tmps4','glbt','tmps7','tmps8','tmps5','yfagz','cb','db','tf','nb','yfagz','yalbx','sybx','yilbx','gjj','gs','tmps6','hdjf','sfgz'
    private function insertGz($data,$year ,$month){
        $model = new Gz;
        //setlocale(LC_ALL, 'zh_CN');
        //echo $data.'<BR />';
        $insertarr = array('tmps2','name','bzgz','mqdays','cqdays','jbgz','tmps4','glbt','tmps7','tmps8','tmps5','yfagz','cb','db','tf','nb','yfagz','yalbx','sybx','yilbx','gjj','gs','tmps6','hdjf','sfgz','tmpi1','tmpi2');
        $data = iconv("GBK","UTF-8//IGNORE",$data);
        //echo $data.'<BR />';
        $line = explode(',',$data);
        if ($line[1]=='') return;//姓名为null
        $model->year   =$year;
        $model->month  =$month;
        $i = 1 ;
        foreach($insertarr as $fieldname) {
            $model->setAttribute( $fieldname , $line[$i]);
            $i++;
        }
        /*
        $model->tmps2  =$line[0];
        $model->name   =$line[1];
        $model->bzgz  =$line[2];
        $model->bzgz   =$line[3];
        $model->tmps4  =$line[4];
        $model->glbt   =$line[5];
        $model->tmps5  =$line[6];
        $model->yfugz  =$line[7];
        $model->cb     =$line[8];
        $model->db     =$line[9];
        $model->tf     =$line[10];
        $model->nb     =$line[11];
        $model->yfagz  =$line[12];
        $model->yalbx  =$line[13];
        $model->sybx   =$line[14];
        $model->yilbx  =$line[15];
        $model->gjj    =$line[16];
        $model->gs     =$line[17];
        $model->tmps6  =$line[18];
        $model->hdjf   =$line[19];
        $model->sfgz   =$line[20];
        */
        
        //echo $line[0].'<BR />';
        //echo $line[2].'<BR />';
        //echo $line[3].'<BR />';
        //echo $line[4].'<BR />';
        $model->save();
    }
    
    
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Gz;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Gz']))
		{
			$model->attributes=$_POST['Gz'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Gz']))
		{
			$model->attributes=$_POST['Gz'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Gz');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Gz('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Gz']))
			$model->attributes=$_GET['Gz'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Gz the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Gz::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Gz $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='gz-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
